package com.lx.nba.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.omg.DynamicAny.DynValueOperations;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.lx.nba.dao.TeamDao;
import com.lx.nba.entiy.Team;
import com.lx.nba.parse.ContentParse;
import com.lx.nba.parse.impl.TeamParseImpl;
import com.lx.nba.service.DownLoadDataService;
import com.lx.nba.service.InitNBAData;
import com.lx.nba.util.Content;
import com.lx.nba.util.ContentUtil;

public class InitNBATeamImpl implements InitNBAData,DownLoadServiceCallback{

	@Autowired
	private ContentParse teamParse ;
	
	@Autowired
	private DownLoadDataService service ;
	
	@Autowired
	private TeamDao teamDao ;
	
	private CountDownLatch latch  ;
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
		Content man = new Content() ;
		
		Content team = new Content() ;
		
		Content range = new Content() ;
		
		range.setUrl("http://matchweb.sports.qq.com/rank/team?competitionId=100000&from=NBA_PC&_=1464143080789");
		
/*		service.download(matchContent(), new DownLoadServiceCallback() {
			
			@Override
			public void callback(Content content) {
				// TODO Auto-generated method stub
				
				System.out.println(content.getContent());
				
				
				
				
			}
		});*/
		
		List<Content> contents = teamListContent() ;
		
		latch = new CountDownLatch(contents.size()) ;
		
		for( Content content : contents){
					
			service.download(content, this);
			
		}
		
		try {
			
			System.out.println("NBA team is initing");
			
			latch.await();
			
			System.out.println("NBA team finish initing");
		
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			System.out.println("init error");
		
		}
		
	}
	
	private Content matchContent(){
		
		String url = "http://matchweb.sports.qq.com/kbs/list?columnId=100000&from=NBA_PC&startTime="+ContentUtil.getDate(-3)+"&endTime="+ContentUtil.getDate(3) ;
		Content match = new Content() ;
		match.setUrl(url);
		
		return match ;
		
	}
	
	private List<Content> teamListContent(){
		
		List<Content> contents = new ArrayList<>() ;
		
		for( int i = 1 ; i <=30 ; i++){
			
			
			Content content = new Content() ;
			content.setUrl("http://ziliaoku.sports.qq.com/cube/index?cubeId=1&dimId=1&params=t1%3A"+i+"&from=sportsdatabase") ;
			contents.add(content) ;
			
		}
		
		
		return contents ;
		
		
	}

	@Override
	public void callback(Content content) {
		// TODO Auto-generated method stub
		
		String text = ContentUtil.getData(content.getContent(), 3) ;

		Gson gson = new Gson() ;
		
		BaseTeam baseTeam = gson.fromJson(text, BaseTeam.class) ;
		
		Team team = new Team() ;
		
		team.setName(baseTeam.getEnName());
		team.setIcon(baseTeam.getLogoNew());
		team.setLocation(baseTeam.getCityEnName());
		team.setBirth(String.valueOf(baseTeam.getJoinNBADate()));
		team.setChampionNum(baseTeam.getHistoryChampion());
		team.setCoach(baseTeam.getCoach());
		team.setTeamId(baseTeam.getTeamId());
		
		teamDao.saveTeam(team);
		
		latch.countDown();
		
	}
		
	class BaseTeam{
		
		private String boss ;
		
		private String brief ;
		
		private String city ;
		
		private String cityEnName ;
		
		private String cnName ;
		
		private String coach ;
		
		private String conference ;
		
		private int curSeasonLosses ;
		
		private int curSeasonRegRank ;
		
		private int curSeasonWins ;
		
		private String division ;
		
		private String divisionSeed ;
		
		private String enName ;
		
		private String fullCnName ;
		
		private int historyChampion ;
		
		private int joinNBADate ;
		
		private String logoBlack ;
		
		private String logoNew ;
		
		private String propsLogo ;
		
		private int teamId ;
		
		private String venue ;

		public String getBoss() {
			return boss;
		}

		public void setBoss(String boss) {
			this.boss = boss;
		}

		public String getBrief() {
			return brief;
		}

		public void setBrief(String brief) {
			this.brief = brief;
		}

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public String getCityEnName() {
			return cityEnName;
		}

		public void setCityEnName(String cityEnName) {
			this.cityEnName = cityEnName;
		}

		public String getCnName() {
			return cnName;
		}

		public void setCnName(String cnName) {
			this.cnName = cnName;
		}

		public String getCoach() {
			return coach;
		}

		public void setCoach(String coach) {
			this.coach = coach;
		}

		public String getConference() {
			return conference;
		}

		public void setConference(String conference) {
			this.conference = conference;
		}

		public int getCurSeasonLosses() {
			return curSeasonLosses;
		}

		public void setCurSeasonLosses(int curSeasonLosses) {
			this.curSeasonLosses = curSeasonLosses;
		}

		public int getCurSeasonRegRank() {
			return curSeasonRegRank;
		}

		public void setCurSeasonRegRank(int curSeasonRegRank) {
			this.curSeasonRegRank = curSeasonRegRank;
		}

		public int getCurSeasonWins() {
			return curSeasonWins;
		}

		public void setCurSeasonWins(int curSeasonWins) {
			this.curSeasonWins = curSeasonWins;
		}

		public String getDivision() {
			return division;
		}

		public void setDivision(String division) {
			this.division = division;
		}

		public String getDivisionSeed() {
			return divisionSeed;
		}

		public void setDivisionSeed(String divisionSeed) {
			this.divisionSeed = divisionSeed;
		}

		public String getEnName() {
			return enName;
		}

		public void setEnName(String enName) {
			this.enName = enName;
		}

		public String getFullCnName() {
			return fullCnName;
		}

		public void setFullCnName(String fullCnName) {
			this.fullCnName = fullCnName;
		}

		public int getHistoryChampion() {
			return historyChampion;
		}

		public void setHistoryChampion(int historyChampion) {
			this.historyChampion = historyChampion;
		}

		public int getJoinNBADate() {
			return joinNBADate;
		}

		public void setJoinNBADate(int joinNBADate) {
			this.joinNBADate = joinNBADate;
		}

		public String getLogoBlack() {
			return logoBlack;
		}

		public void setLogoBlack(String logoBlack) {
			this.logoBlack = logoBlack;
		}

		public String getLogoNew() {
			return logoNew;
		}

		public void setLogoNew(String logoNew) {
			this.logoNew = logoNew;
		}

		public String getPropsLogo() {
			return propsLogo;
		}

		public void setPropsLogo(String propsLogo) {
			this.propsLogo = propsLogo;
		}

		public int getTeamId() {
			return teamId;
		}

		public void setTeamId(int teamId) {
			this.teamId = teamId;
		}

		public String getVenue() {
			return venue;
		}

		public void setVenue(String venue) {
			this.venue = venue;
		}
		
		
	}
}
